import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules（如果路由过长，可以定义在外部，引入后使用） */
// import tableRouter from './modules/table'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes（常量路由）
 * a base page that does not have permission requirements（不需要权限验证）
 * all roles can be accessed（所有用户都能访问）
 */
export const constantRoutes = [
  // 重定向
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },

  // 登录页面
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 未登录，重定向到登录界面
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },

  // 404页面
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },

  // 401页面
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  // 根路径
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', affix: true, noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes（动态路由）
 * the routes that need to be dynamically loaded based on user roles（需要根据用户角色动态加载的路由）
 */
export const asyncRoutes = [
  // 任务管理
  {
    path: '/task',
    component: Layout,
    redirect: '/task',
    name: 'Task',
    meta: { title: 'task', icon: 'task', roles: ['admin', 'keeper'] },
    children: [
      {
        path: 'input',
        name: 'Input',
        component: () => import('@/views/task/input'),
        meta: { title: 'input', icon: 'input', noCache: true }
      },
      {
        path: 'output',
        name: 'Output',
        component: () => import('@/views/task/output'),
        meta: { title: 'output', icon: 'output', noCache: true }
      },
      {
        path: 'transfer',
        name: 'Transfer',
        component: () => import('@/views/task/transfer'),
        meta: { title: 'tTransfer', icon: 'tTransfer', noCache: true }
      },
      {
        path: 'check',
        name: 'Check',
        component: () => import('@/views/task/check'),
        meta: { title: 'tCheck', icon: 'tCheck', noCache: true }
      }
    ]
  },

  // 货物采购
  {
    path: '/purchase',
    component: Layout,
    redirect: '/purchase',
    name: 'Purchase',
    meta: { title: 'purchase', icon: 'el-icon-sell', roles: ['admin', 'buyer'] },
    children: [
      {
        path: 'pOrder',
        name: 'POrder',
        component: () => import('@/views/purchase/pOrder'),
        meta: { title: 'pOrder', icon: 'pOrder', noCache: true }
      }
      // ,
      // {
      //   path: 'pChargeback',
      //   name: 'PChargeback',
      //   component: () => import('@/views/purchase/pChargeback'),
      //   meta: { title: 'pChargeback', icon: 'pChargeback' }
      // }
    ]
  },

  // 货物销售
  {
    path: '/sale',
    component: Layout,
    redirect: '/sale',
    name: 'Sale',
    meta: { title: 'sale', icon: 'el-icon-sold-out', roles: ['admin', 'salesperson'] },
    children: [
      {
        path: 'sOrder',
        name: 'SOrder',
        component: () => import('@/views/sale/sOrder'),
        meta: { title: 'sOrder', icon: 'sOrder', noCache: true }
      }
      // ,
      // {
      //   path: 'sChargeback',
      //   name: 'SChargeback',
      //   component: () => import('@/views/sale/sChargeback'),
      //   meta: { title: 'sChargeback', icon: 'sChargeback' }
      // }
    ]
  },

  // 信息管理
  {
    path: '/information',
    component: Layout,
    redirect: '/information',
    name: 'Information',
    meta: { title: 'information', icon: 'el-icon-coin', roles: ['admin', 'keeper'] },
    children: [
      {
        path: 'goods',
        name: 'Goods',
        // bug-fixed：嵌套路由的父路由必须包含index.vue页面，内容为 <router-view />
        component: () => import('@/views/information/goods'),
        meta: { title: 'goods', icon: 'goods' }
      },
      {
        path: 'brand',
        name: 'Brand',
        component: () => import('@/views/information/brand'),
        meta: { title: 'brand', icon: 'brand' }
      },
      {
        path: 'unit',
        name: 'Unit',
        component: () => import('@/views/information/unit'),
        meta: { title: 'unit', icon: 'unit' }
      },
      {
        path: 'warehouse',
        name: 'Warehouse',
        component: () => import('@/views/information/warehouse'),
        meta: { title: 'warehouse', icon: 'warehouse' }
      },
      {
        path: 'customer',
        name: 'Customer',
        component: () => import('@/views/information/customer'),
        meta: { title: 'customer', icon: 'customer' }
      },
      {
        path: 'supplier',
        name: 'Supplier',
        component: () => import('@/views/information/supplier'),
        meta: { title: 'supplier', icon: 'supplier' }
      }
    ]
  },

  // 账户管理
  {
    path: '/account',
    component: Layout,
    redirect: '/account',
    name: 'Account',
    meta: { title: 'account', icon: 'el-icon-user', roles: ['admin'] },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/account/user'),
        meta: { title: 'user', icon: 'users', noCache: true }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
